package com.zx.tradeclient.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.adapter.SecondCategoryAdapter;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index2.Index2Activity;
import com.zx.tradeclient.ctrl.index3.Index3Activity;
import com.zx.tradeclient.ctrl.index4.Index4Activity;
import com.zx.tradeclient.ctrl.index5.DetialPageActivity;
import com.zx.tradeclient.ctrl.index5.GoodsEditActivity;
import com.zx.tradeclient.ctrl.index5.UserCenterActivity;
import com.zx.tradeclient.entity.CategoryInfo;

public class SecondCategoryActivity extends BaseActivity implements OnClickListener {

	private ListView list;
	private SecondCategoryAdapter adapter;
	private ArrayList<CategoryInfo> list_info = new ArrayList<CategoryInfo>();

	private TradeApp app;

	private ProgressDialog progressDialog;

	private String pID = "";
	private int categ = -1;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:

				progressDialog = ProgressDialog.show(SecondCategoryActivity.this, null, "正在获取数据，请稍后.....", true, true);
				getCityInfo();

				break;
			case 1:

				progressDialog.dismiss();
				adapter.notifyDataSetChanged();

				break;
			case 2:
				progressDialog.dismiss();

				Toast toast2 = Toast.makeText(SecondCategoryActivity.this, "无反应", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

				break;

			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_activity);

		if (!(ActivityTaskManager.getInstance().containsName("SecondCategoryActivity"))) {
			ActivityTaskManager.getInstance().putActivity("SecondCategoryActivity", this);
		}

		init();
	}

	private void init() {

		app = (TradeApp) getApplication();

		pID = getIntent().getStringExtra("provinceId");
		categ = Integer.parseInt(getIntent().getStringExtra("categ"));

		setHeader(0);

		header_right = (Button) findViewById(R.id.header_right);
		header_right.setBackgroundResource(R.drawable.header_dnone);
		header_right.setOnClickListener(this);
		header_right.setVisibility(View.VISIBLE);

		if (0 == categ) {
			header_right.setText(R.string.header_right_category);
		} else if (1 == categ) {
			header_right.setText(R.string.header_right_area);
		}

		adapter = new SecondCategoryAdapter(this, list_info);

		list = (ListView) findViewById(R.id.category_list);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

				switch (app.categ_index) {
				case 1:

					break;
				case 2:
					Intent intent2 = new Intent(SecondCategoryActivity.this, Index2Activity.class);
					startActivity(intent2);
					break;
				case 3:
					Intent intent3 = new Intent(SecondCategoryActivity.this, Index3Activity.class);
					startActivity(intent3);
					break;
				case 4:
					Intent intent4 = new Intent(SecondCategoryActivity.this, Index4Activity.class);
					startActivity(intent4);
					break;
				case 5:

					Intent intent5 = new Intent(SecondCategoryActivity.this, UserCenterActivity.class);
					startActivity(intent5);

					break;

				case 7:
					Intent intent7 = new Intent(SecondCategoryActivity.this, DetialPageActivity.class);
					startActivity(intent7);
					break;

				case 8:
					break;

				default:
					Intent intent8 = new Intent(SecondCategoryActivity.this, GoodsEditActivity.class);
					startActivity(intent8);
					break;
				}

			}
		});

		Message msg0 = new Message();
		msg0.what = 0;
		handler.sendMessage(msg0);

	}

	private void getCityInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		if (0 == categ) {
			map.put("act", "productTypeList");
		} else if (1 == categ) {
			map.put("act", "provinceCity");
		}
		map.put("parentId", pID);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				Message msg2 = new Message();
				msg2.what = 2;
				handler.sendMessage(msg2);
			}
		});

	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				String data = response.getString("dataList");

				JSONArray list = new JSONArray(data);

				for (int i = 0; i < list.length(); i++) {
					CategoryInfo info = new CategoryInfo();
					JSONObject log = list.getJSONObject(i);
					String cat_id = log.getString("id");
					String cat_name = log.getString("name");

					info.setCategory_id(cat_id);
					info.setCategory_name(cat_name);

					list_info.add(info);
				}
				Message msg1 = new Message();
				msg1.what = 1;
				handler.sendMessage(msg1);

			} else {
				Message msg2 = new Message();
				msg2.what = 2;
				handler.sendMessage(msg2);
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {

			goback();

		} else if (v == header_right) {
			if (getString(R.string.header_right_category).equals(header_right.getText().toString())) {
				header_right.setText(R.string.header_right_area);
			} else if (getString(R.string.header_right_area).equals(header_right.getText().toString())) {
				header_right.setText(R.string.header_right_category);
			}
		}

	}

}
